<template>
  <div id="search">
    <div class="bgc">
      <van-row>
        <!-- 顶部标题 -->
        <van-col span="24">
          <van-nav-bar title="搜索" left-arrow @click-left="onClickLeft" />
        </van-col>
        <!-- 搜索框 -->
        <van-col span="24">
          <form action="/">
            <van-search v-model="value" show-action placeholder="520礼包抢先领" @input="onChangeValue(value)" />
          </form>
        </van-col>
        <!-- 热门搜索 -->
        <van-col span="24" class="column" v-if="value == ''">
          <div id="default">
            <p>热门搜索</p>
            <van-col span="24" class="default-btn">
              <van-button type="danger" @click="onColumnSpan()" size="mini"> {{ defaultKeyword.keyword }} </van-button>
              <!-- v-for="(item, index) in HistoryList" :key="index" -->
            </van-col>
            <van-col span="24" class="default-btn">
              <van-button type="info" plain @click="onColumnSpan(item)" size="mini" v-for="(item, index) in historyKeywordList" :key="index"> {{ item.keyword }} </van-button>
            </van-col>
            <van-col span="24" class="del">
              <van-button plain size="mini" type="warning" icon="delete-o" @click="onColumnEmpty">清空记录</van-button>
            </van-col>
          </div>
        </van-col>
        <!-- 搜索结果 -->
        <van-col v-if="value !== ''" span="24">
          <div id="complete">
            <van-row class="complete-centenr">
              <van-sticky :offset-top="0">
                <van-col span="24" class="complete-centenr-bar">
                  <van-col span="8" :class="num == 1 ? 'selected' : ''" @click="num = 1">综合</van-col>
                  <van-col span="8" :class="num == 2 ? 'selected' : ''" @click="num = 2">
                    价格
                    <span @click="onSort">{{ sort == false ? '∧' : '∨' }}</span>
                  </van-col>
                  <van-col span="8" :class="classStatus == true ? 'selected' : ''" @click="classStatus == false ? (classStatus = true) : (classStatus = false)" v-model="classStatus">分类</van-col>
                </van-col>
              </van-sticky>
              <!-- 综合产品栏 -->
              <van-col span="24" class="complete-centenr-comprehensive" v-if="num == 1">
                <van-col span="12" v-for="(item, index) in GetCategoryList" :key="index" @click="onGoGoodsList(item)">
                  <div>
                    <img :src="item.picUrl" alt="" />
                    <p>{{ item.name }}</p>
                    <p>￥{{ item.retailPrice }}</p>
                  </div>
                </van-col>
              </van-col>
              <!-- 价格栏 -->
              <van-col span="24" class="complete-centenr-comprehensive" v-if="num == 2">
                <van-col span="12" v-for="(item, index) in GetDescList.list" :key="index" @click="onGoGoodsList(item)">
                  <div>
                    <img :src="item.picUrl" alt="" />
                    <p>{{ item.name }}</p>
                    <p>￥{{ item.retailPrice }}</p>
                  </div>
                </van-col>
              </van-col>
              <!-- 分类栏 -->
              <van-col span="24" v-if="classStatus == true" class="drop-down">
                <span v-for="(item, index) in GetCategory.filterCategoryList" @click="onValueClass(item)">{{ item.name }}</span>
              </van-col>
            </van-row>
          </div>
        </van-col>
        <van-co span="24" v-if="nullStatus">
          <van-empty image="search" description="暂无商品" />
        </van-co>
      </van-row>
      <!-- <van-empty v-if="$store.state.valueList.length >= 2" image="search" :description="'无 ‘' + value + '’商品信息'" /> -->
    </div>
  </div>
</template>
<script>
export default {
  name,
  data() {
    return {
      value: '',
      // 切换栏的状态值
      num: 1,
      // 分类下拉的状态
      classStatus: false,
      // 价格排序的 状态
      sort: false,
      // 降序
      GetDescList: {},
      // 升序
      GetAscList: {},
      // 全部的分类内容
      GetCategory: {},
      GetCategoryList: {},
      //  获取热门搜索以及
      defaultKeyword: {},
      // 搜索历史
      hotKeywordList: {},
      // 搜索无内容要展示的
      nullStatus: false
    }
  },
  computed: {},
  mounted() {
    this.onGetClassList()
    this.onGetHistoryList()
  },
  methods: {
    // 返回上一级
    onClickLeft() {
      this.$router.back()
    },
    // 监听搜索框里面的内容并且请求对应的参数
    onChangeValue(value) {
      const data = {
        keyword: value,
        page: 1,
        limit: 10
      }
      this.$get('goods/list', data)
        .then(result => {
          this.GetCategory = result.data.data
          this.GetCategoryList = result.data.data.list
        })
        .catch(err => {})
      if (this.GetCategory.length == 0) {
        this.nullStatus = true
      }
    },
    // 跳转对应的商品详情，并且获取当前商品的 详情信息
    onGoGoodsList(item) {
      let params = {
        id: item.id
      }
      this.$router.push({ path: '/goodslist', query: params })
    },
    // 监听价格排序
    onSort() {
      if (this.sort) {
        this.sort = false
        //降序
        const data = {
          keyword: this.value,
          page: 1,
          limit: 10,
          sort: 'retail_price',
          order: 'desc'
        }
        this.$get('goods/list', data)
          .then(result => {
            this.GetDescList = result.data.data
          })
          .catch(err => {})
      } else {
        //降序
        this.sort = true
        //降序
        const data = {
          keyword: this.value,
          page: 1,
          limit: 10,
          sort: 'retail_price',
          order: 'asc'
        }
        this.$get('goods/list', data)
          .then(result => {
            this.GetDescList = result.data.data
          })
          .catch(err => {})
      }
    },
    // 获取分类
    onGetClassList() {
      this.$get('goods/category')
        .then(result => {})
        .catch(err => {})
    },
    // 下拉分类 的类目 搜索
    onValueClass(item) {
      this.classStatus = false
      this.value = item.name
      const data = {
        keyword: this.value,
        page: 1,
        limit: 10
      }
      this.$get('goods/list', data)
        .then(result => {
          this.GetCategoryList = result.data.data
        })
        .catch(err => {})
    },
    // 点击热门搜索的事件
    onColumnSpan(item) {
      let keyword = item.keyword.toString()
      this.value = keyword
      this.onChangeValue(keyword)
    },
    // 清空历史记录
    onColumnEmpty() {
      this.$post('search/clearhistory').then(res => {
        this.onGetHistoryList()
      })
    },
    // 获取热门搜索以及 搜索历史
    onGetHistoryList() {
      this.$get('search/index')
        .then(result => {
          console.log(result)
          this.defaultKeyword = result.data.data.defaultKeyword
          this.historyKeywordList = result.data.data.historyKeywordList
        })
        .catch(err => {})
    }
  }
}
</script>
<style lang="less" scoped>
.column {
  background-color: #fff;
  border-top: 3px solid #e6e7e9;
  .van-col {
    margin: 10px 0px;
    font-size: 0.13rem;
    span {
    }
  }
}
.selected {
  color: red;
}
#default {
  padding: 0.1rem;
  background-color: #fff;
  .default-btn {
    .van-button {
      margin: 10px 10px 0px 0px;
    }
  }
  p {
    font-size: 16px;
    opacity: 0.8;
  }
  .del {
    text-align: center;
    .van-button {
      font-size: 0.1rem;
    }
  }
}
#complete {
  width: 100%;
}
.complete-centenr {
  position: absolute;
  width: 100%;
  .complete-centenr-bar {
    border-bottom: 3px solid #e6e7e9;
    text-align: center;
    .van-col {
      line-height: 0.5rem;
      font-size: 0.15rem;
      background-color: #fff;
    }
  }
  .complete-centenr-comprehensive {
    background-color: #f4f4f4;
    .van-col {
      padding: 2px;
      div {
        width: 100%;
        height: 2.5rem;
        background-color: #fff;
        text-align: center;
        img {
          width: 90%;
        }
        p:nth-child(2) {
          margin: 0px;
          font-size: 15px;
        }
        p:nth-child(3) {
          margin: 0px;
          color: chocolate;
          margin-top: 10px;
        }
      }
    }
  }
  .drop-down {
    position: absolute;
    top: 53px;
    background-color: #fff;
    height: 100px;
    padding: 20px;
    border-bottom: 3px solid #e6e7e9;
    span {
      padding: 3px 5px;
      background-color: #fff;
      border: 1px solid rgba(0, 0, 0, 0.5);
      color: rgba(0, 0, 0, 0.5);
      font-size: 0.1rem;
      margin-right: 0.2rem;
    }
  }
}
</style>
